<template>
<div>
    <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :labelWidth="110"
        :formType="type"
        :isSubmitBtn="true"
        @formChange="formChangeHandle"
    />
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      width="700"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <dealer :type="handleType" :rowData="formData" @close="closeHandler" @reAdd='reAdd'/>
    </BaseDialog>
</div>
</template>
<script>
    import dealer from './dealer/dealer';
    import { updateBusinessCustomer,customersSelect } from '@/api/part/partInfoManage/contactsCustomer';
    export default{
        components:{
            dealer
        },
        props: {
            type: {
                type: String,
                default: 'add'
            },
            rowData: {
                type: Object,
                default: () => {}
            }
        },
        data(){
            return {
                formList:this.createColumns(),
                visible:false,
                dialogTitle:'',
                formData:{},
                handleType:''
            };
        },
        methods:{
            createColumns(){
                return [
                    {
                        type: 'INPUT',
                        label: '客户代码',
                        fieldName: 'customerCode',
                        disabled:true
                    },
                    {
                        type: 'SELECT',
                        label: '客户类型名称',
                        fieldName: 'customerTypeCode',
                        itemList: []
                    },
                    {
                        type: 'INPUT',
                        label: '客户名称',
                        fieldName: 'customerName',
                        selfCols:2
                    },
                    {
                        type: 'INPUT',
                        label: '客户拼音',
                        fieldName: 'customerSpell',
                        disabled:true
                    },
                    {
                        type: 'INPUT',
                        label: '经销商',
                        fieldName: 'dealerShortName',
                        unitRender: () => {
                            return (
                                <el-button size="mini" type="text" onClick={() => this.searchDealer()}>搜索</el-button>
                            )
                        }
                    },
                    {
                        type: 'INPUT',
                        label: '客户简称',
                        fieldName: 'customerShortName'
                    },
                    {
                        type: 'INPUT',
                        label: '邮编',
                        fieldName: 'zipCode'
                    },
                    {
                        type: 'INPUT',
                        label: '地址',
                        fieldName: 'address',
                        selfCols:2
                    },
                    {
                        type: 'INPUT',
                        label: '联系人',
                        fieldName: 'contactorName'
                    },
                    {
                        type: 'INPUT',
                        label: '联系人电话',
                        fieldName: 'contactorPhone'
                    },
                    {
                        type: 'INPUT',
                        label: '传真',
                        fieldName: 'fax'
                    },
                    {
                        type: 'INPUT',
                        label: '联系人手机',
                        fieldName: 'contactorMobile'
                    },
                    {
                        type: 'INPUT',
                        label: '合约编号',
                        fieldName: 'contractNo'
                    },
                    {
                        type: 'SELECT',
                        label: '客户收款类别',
                        fieldName: 'cusReceiveSort',
                        itemList: [{value:'1',text:'客户付费'},{value:'2',text:'保险付费'},
                        {value:'3',text:'OEM索赔'},{value:'4',text:'4S点付费'},{value:'5',text:'其他索赔'}]
                    },
                    {
                        type: 'DATE',
                        label: '签约开始日期',
                        fieldName: 'agreementBeginDate'
                    },
                    {
                        type: 'DATE',
                        label: '签约结束日期',
                        fieldName: 'agreementEndDate'
                    },
                    {
                        type: 'INPUT',
                        label: '订货周期',
                        fieldName: 'leadTime'
                    },
                    {
                        type: 'INPUT',
                        label: '价格系数',
                        fieldName: 'priceRate'
                    },
                    {
                        type: 'INPUT',
                        label: '销售基价',
                        fieldName: 'salesBasePriceType'
                    },
                    {
                        type: 'INPUT',
                        label: '维修欠款',
                        fieldName: 'arrearageAmount'
                    },
                    {
                        type: 'INPUT',
                        label: '账龄',
                        fieldName: 'accountAge'
                    },
                    {
                        type: 'INPUT',
                        label: '加价率',
                        fieldName: 'priceAddRate'
                    },
                    {
                        type: 'INPUT',
                        label: '数据来源',
                        fieldName: 'oemTag'
                    },
                    {
                        type: 'INPUT',
                        label: '税号',
                        fieldName: 'taxNo'
                    }
                ]
            },
            async formChangeHandle(val){
                console.log(val);
                let res = await updateBusinessCustomer(val);
                console.log(res);
                this.$emit('reEdit', false, this.arr);
            },
            searchDealer(){
                this.visible = true;
            },
            closeHandler(){

            },
            reAdd(x, y){
                console.log(x, y);
                for(let key in y){
                    for(let i = 0; i < this.formList.length; i++){
                        if(this.formList[i].fieldName == 'dealerShortName'){
                            this.formList[i].initialValue = y['dealerShortName'];
                        }
                    }
                }
                this.visible = false;
            },
            async customersSelect(){
                let res = await customersSelect();
                let json = res.data.rows.map( s => ({
                    value:s.customerTypeCode,
                    text: s.customerTypeName
                }));
                console.log(json)
                for(let j = 0; j < this.formList.length; j++){
                    if(this.formList[j].fieldName == 'customerTypeCode'){
                        this.formList[j].itemList = json;
                        return;
                    }
                }
            }
        },
        mounted(){
            console.log(this.rowData);
            for(let i in this.rowData){
                for(let j = 0; j < this.formList.length; j++){
                    if(this.formList[j].fieldName == i){
                        this.formList[j].initialValue = this.rowData[i];
                    }
                }
            };
            this.customersSelect();
        }
    }
</script>
<style>
</style>
